
#include "SDL.h"
#include "sprig.h"
#include "../jel.h"
#include <iostream>
#include <string>
using namespace std;

SDL_Surface* screen;

void printIt(string str)
{
    printf(str.c_str());
}

void colorChange(Uint32 color)
{
    SDL_FillRect(screen, NULL, color);
}

void start()
{
    using namespace JEL;
    
    Signal1<void, string>* sig = new Signal1<void, string>;
    sig->setFn(printIt);
    sig->setArg1("Heya\n");
    
    Schedule schedule;
    ActionEventID e = schedule.addActionEvent(ActionEvent(sig));
    
    schedule.triggerAction(e);

    // Lots of testing!
    for(int i = 0; i < 200; i++)
    {
        sig = new Signal1<void, string>;
        sig->setFn(printIt);
        Uint32 time = rand()%5000;
        
        char buff[100];
        sprintf(buff, "  %d", time);
        sig->setArg1(buff);
        
        schedule.addTimeEvent(TimeEvent(sig), time);
    }


    
    Signal1<void, Uint32>* sig2 = new Signal1<void, Uint32>;
    sig2->setFn(colorChange);
    sig2->setArg1(SDL_MapRGB(screen->format, 255, 0, 0));
    
    schedule.addTimeEvent(TimeEvent(sig2), 2000);
    
    sig2 = new Signal1<void, Uint32>;
    sig2->setFn(colorChange);
    sig2->setArg1(SDL_MapRGB(screen->format, 0, 255, 0));
    
    schedule.addTimeEvent(TimeEvent(sig2), 3000);
    
    sig2 = new Signal1<void, Uint32>;
    sig2->setFn(colorChange);
    sig2->setArg1(SDL_MapRGB(screen->format, 0, 0, 255));
    
    schedule.addTimeEvent(TimeEvent(sig2), 4000);
    
    sig2 = new Signal1<void, Uint32>;
    sig2->setFn(colorChange);
    sig2->setArg1(SDL_MapRGB(screen->format, 128, 0, 128));
    
    schedule.addTimeEvent(TimeEvent(sig2), 4500);
    
    sig2 = new Signal1<void, Uint32>;
    sig2->setFn(colorChange);
    sig2->setArg1(SDL_MapRGB(screen->format, 128, 128, 0));
    
    TimeEventID id = schedule.addTimeEvent(TimeEvent(sig2), 5000);
    
    sig2 = new Signal1<void, Uint32>;
    sig2->setFn(colorChange);
    sig2->setArg1(SDL_MapRGB(screen->format, 0, 0, 0));
    
    schedule.addTimeEvent(TimeEvent(sig2), 2500);

    //schedule.setTime(2500);
    
    //schedule.clearTimeEvents();


    Uint32 frametime;
    
    float targetFps = 60;
    float waitTime = 1000.0f / targetFps;
    
    SDL_FillRect(screen, NULL, 0x7171C6);
    
    int mx = 0, my = 0;
    
    bool done = 0;
    SDL_Event event;
    int frameStart;
    Uint32 dt = 20;
    Uint32 totalTime = 0;
    while (!done)
    {
        frameStart = SDL_GetTicks();
        SDL_GetMouseState(&mx, &my);
        
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
                done = 1;
            

            if (event.type == SDL_KEYDOWN)
            {
                if (event.key.keysym.sym == SDLK_q)
                    done = 1;
            }
        }
        
        schedule.update(dt);
	
        totalTime += dt;
        printf("\nTIME: %d\n", totalTime);
        
        SDL_Flip(screen);
        
        frametime = SDL_GetTicks() - frameStart;

        if (SDL_GetTicks() - frameStart < waitTime)
            SDL_Delay((int)(waitTime - frametime));

    }
    
    printf("\nGetting a time event by ID... id = %d\n", id.getID());
    TimeEvent te = schedule.getTimeEvent(id);
    printf("\nGot it.  Time: %d\n", te.time);
    
    
    vector<TimeEvent> tEvents = schedule.getAllTimeEvents();
    printf("\nClearing past events...  Time now: %d\n", schedule.getTime());
    printf("Events before deletion: %d\n", tEvents.size());
    schedule.clearPastTimeEvents();
    tEvents = schedule.getAllTimeEvents();
    printf("Time events after deletion: %d\n", tEvents.size());
    
    printf("Stress-testing adding and clearing events.\n");
    for(int j = 0; j < 50; j++)
    {
        for(int i = 0; i < 200; i++)
        {
            Uint32 time = rand()%5000;
            
            schedule.addTimeEvent(TimeEvent(), time);
        }
        schedule.clearPastTimeEvents();
    }
    printf("Done.\n");
    
}







int main(int argc, char* argv[])
{
    
    char buff[] = "SDL_VIDEO_CENTERED=center";
    
    SDL_putenv(buff);
    
    screen = SPG_InitSDL(800, 600, 32, SDL_INIT_VIDEO, SDL_SWSURFACE);
    if(screen == NULL)
        return 0;
    
    SDL_WM_SetCaption("GRO Gui Demo1", NULL);
    
    srand(time(NULL));
    start();
    
    SDL_Quit();
    return 0;
}
